public class test15 {
    public List<Integer> spiralOrder(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        Integer[] res = new Integer[m*n];
        int l = 0,r = n-1;
        int t = 0,b = m-1;
        int x = 0;
        while(true){
            //左-右
            for(int i = l;i<=r;i++){
                res[x++] = matrix[t][i];
            }
            //++t;
            if(++t>b)break;
            //上-下
            for(int i = t;i<=b;i++){
                res[x++] = matrix[i][r];
            }
            if(--r<l)break;
            //右-左
            for(int i = r;i>=l;i--){
                res[x++] = matrix[b][i];
            }
            if(--b<t)break;
            //下-上
            for(int i = b;i>=t;i--){
                res[x++] = matrix[i][l];
            }
            if(++l>r)break;
        }
        return Arrays.asList(res);
    }
}
